import type { Component, PropType } from 'vue'

import { FieldRule } from 'vant'

import VCheckBox from '../VCheckBox/VCheckBox.vue'
import VRadio from '../VRadio/VRadio.vue'
import VSwitch from '../VSwitch/VSwitch.vue'
import VSlider from '../VSlider/VSlider.vue'
import VInput from '../VInput/VInput.vue'
import VCalendar from '../VCalendar/VCalendar.vue'

export type FormComponents =
  | 'VCheckBox'
  | 'VRadio'
  | 'VSwitch'
  | 'VSlider'
  | 'VInput'
  | 'Field'
  | 'VCalendar'

export const formComponents = {
  VCheckBox,
  VRadio,
  VSwitch,
  VSlider,
  VInput,
  VCalendar
}

export type FormFieldName =
  | 'VCheckBox'
  | 'VRadio'
  | 'VSwitch'
  | 'VSlider'
  | 'VInput'
  | 'VCalendar'

export const definePropType = <T>(val: any): PropType<T> => val

export interface FormFields {
  el: FormComponents | Component
  label: string
  key: string
  name: FormFieldName
  rules?: FieldRule[]
  attrs?: Record<string, any>
  useField?: boolean
}

export default {
  fields: definePropType<FormFields[]>(Array),
  hiddenInActions: {
    type: Boolean,
    default: false
  },
  submitText: {
    type: String,
    default: '提交'
  },
  labelWidth: {
    type: [Number, String],
    default: '4em'
  },
  value: {
    type: Object,
    default: () => {}
  }
} as const

export const formEmits = {
  getFormValidate: (val: Record<string, any>) => !!val
}
